package dd.prog2.exercises.set12.a1;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class VierGewinnt{

	public static void main(String[] args) {
		new VierGewinnt().start();
	}

	public void start() {
		// buttons & texfield
		final JButton reset = new JButton("Start new game");
		final JButton[] buttons = new JButton[42];
		final JTextField status = new JTextField("Status: waiting for the player 1 turn");
		final JTextArea info = new JTextArea(("WHITE: next possible turn\nRED: Player 1\nYELLOW: Player 2"));
		// board object
		final Board myBoard = new Board();
		// BEGIN local ActionListener
		class ButtonListener implements ActionListener {
			JButton b;
			int i;
			public ButtonListener(JButton b, int i){
				this.b = b;
				this.i = i;
			}
			
			void guiReset(){
				for (int i = 0; i < buttons.length; i++) {
//					buttons[i].setBackground(null); // remove color
					buttons[i].setText("");
					if(i>34){
						buttons[i].setEnabled(true); // re-enable bottom buttons
						buttons[i].setBackground(Color.WHITE);
					} else {
						buttons[i].setEnabled(false); // disable other buttons
						buttons[i].setBackground(Color.GRAY);
					}
					status.setText("Status: Starting new Game. waiting for Player 1");
				}
			}
			
			@Override
			public void actionPerformed(ActionEvent ae) {

				if(ae.getSource() == reset){
					//reset board
					myBoard.reset(); 
					guiReset();
				} else {
					//playing
					if(!myBoard.getGameOver()){
						if( ((i>34) && (i<42)) || ((i<35) && (myBoard.get(i+7) != 0)) ){
							// set color depending on player
							if(myBoard.getPlayer() == 1){
								b.setBackground(Color.RED);
							} else 
								b.setBackground(Color.YELLOW);
							b.setEnabled(false);// disable button
							b.setText(""+myBoard.getPlayer());
							myBoard.setTurn(i);// save turn to board
							if(i-7 >= 0){
								buttons[i-7].setEnabled(true);
								buttons[i-7].setBackground(Color.WHITE);
							}
							status.setText("Status: waiting for the player "+ myBoard.getPlayer()+" turn");
						}
					} 
					if(myBoard.getGameOver()){ // game over?
						if(myBoard.getWinner() != 0) // check winner
							status.setText("Status: Game Over. Winner is Player "+myBoard.getWinner());
						else 
							status.setText("Status: Game Over. No Winner");
					}

				}
			}
		}
		// END Local ActionListener
		JFrame frame = new JFrame("Vier gewinnt v1.0"); // create Frame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(null);
		frame.setLocation(250, 250);
		frame.setResizable(false);
		frame.setSize(305, 370);
		
		Panel buttonPanel = new Panel(); // buttons
		buttonPanel.setLayout(new GridLayout(6,7));
		buttonPanel.setSize(305, 250);
		buttonPanel.setLocation(0, 0);
		for (int i = 0; i < buttons.length; i++) {
			buttons[i] = new JButton("");
			buttons[i].addActionListener(new ButtonListener(buttons[i],i));
			if(i<35){
				buttons[i].setEnabled(false); // disable other buttons
				buttons[i].setBackground(Color.GRAY);
			} else {
				buttons[i].setEnabled(true);
				buttons[i].setBackground(Color.WHITE);
			}
			buttonPanel.add(buttons[i]);
		}
		
		status.setLocation(0, 245); //textfield
		status.setSize(305,20);
		status.setEditable(false);
		status.setHorizontalAlignment(JTextField.LEFT);
		
		reset.setLocation(0, 265); // reset button
		reset.setSize(300, 25);
		reset.addActionListener(new ButtonListener(reset,-1));

		info.setLocation(0, 290); //textfield
		info.setSize(305,60);
		info.setEditable(false);
//		info.setHorizontalAlignment(JTextField.LEFT);
		
		frame.add(status);
		frame.add(buttonPanel);
		frame.add(reset);
		frame.add(info);
		
		frame.setVisible(true);
		

	}
	
}
